package com.future.dao;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.future.model.po.UserModel;

public class UserDAO extends BaseDAO<UserModel> {

	private static Logger logger = LogManager.getLogger(UserDAO.class);

	public UserDAO() {

	}

	public UserDAO(Connection conn) {
		super(conn);
	}

	public boolean saveUser(UserModel user) {
		boolean flag = false;
		String sql = "insert into user (username , password , email, phone , unit , departmentid , areaid , realname,registTel ) values (?,?,?,?,?,?,?,?,?)";
		flag = this.update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getPhone(), user.getUnit(), user.getDepartmentId(),
				user.getAreaId(), user.getRealName(), user.getRegisterTel());
		return flag;
	}

	public boolean modifyUser(UserModel user) {
		boolean flag = false;
		String sql = "update user set username = ? , password = ? , email = ? , phone = ? , unit = ? , realname = ? where id = ? ";
		this.update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getPhone(), user.getUnit(), user.getRealName(), user.getId());
		return flag;
	}

	public UserModel findById(Integer userId) throws Exception {
		String sql = "select * from user where id = ? ";
		List<UserModel> list = this.searchAsBeanList(sql, UserModel.class, userId);
		if (list.size() < 1) {
			throw new Exception("user表没有记录");
		}
		UserModel user = list.get(0);
		return user;
	}

	public List<UserModel> findByUsername(String username) throws Exception {
		String sql = "select * from user where username = ? ";
		List<UserModel> list = this.searchAsBeanList(sql, UserModel.class, username);

		int resultCount = list.size();
		if (resultCount > 1) {
			throw new Exception("user表中存在重名记录");
		}
		return list;
	}

	public UserModel findUser(String username, String password) throws Exception {
		String sql = "select * from user where username = ? and password = ? ";
		List<UserModel> list = this.searchAsBeanList(sql, UserModel.class, username, password);
		int resultCount = list.size();
		if (resultCount < 1) {
			throw new Exception("未能在user表中找到记录");
		} else if (resultCount > 1) {
			throw new Exception("在user表中找到多条记录");
		}
		return list.get(0);
	}
	public UserModel findUser(String username) throws Exception {
		String sql = "select * from user where username = ? ";
		List<UserModel> list = this.searchAsBeanList(sql, UserModel.class, username);
		int resultCount = list.size();
		if (resultCount < 1) {
			throw new Exception("未能在user表中找到记录");
		} else if (resultCount > 1) {
			throw new Exception("在user表中找到多条记录");
		}
		return list.get(0);
	}

	public List<UserModel> find(Map<String, Object> condition) {
		StringBuffer sb = new StringBuffer("select * from user where ");
		List<Object> values = new ArrayList<Object>();

		Iterator<String> columns = condition.keySet().iterator();
		while (columns.hasNext()) {
			String column = columns.next();
			sb.append(column + " = ? ");
			values.add(condition.get(column));
			if (columns.hasNext()) {
				sb.append(" and ");
			}
		}

		Object[] params = values.toArray();
		String sql = sb.toString();
		logger.debug(sql);
		logger.debug(params);
		return this.searchAsBeanList(sql, UserModel.class, params);
	}
	/**
	 * 登录时判断用户名是否存在
	 * petter 20140224
	 * @param username
	 * @return true  用户名存在  false  用户名不存在
	 */
	public boolean findUsername(String username){
		String sql = "select * from user where username = ? ";
		List<UserModel> list = this.searchAsBeanList(sql, UserModel.class, username);
		int resultCount = list.size();
		return  resultCount>0?true:false;
	}
	/**
	 * 登录时判断密码是否存在
	 * petter 20140224
	 * @param username
	 * @return true  密码正确  false  密码错误
	 */
	public boolean findpassword(String username,String password){
		String sql = "select * from user where username = ? and password=?";
		List<UserModel> list = this.searchAsBeanList(sql, UserModel.class, username,password);
		int resultCount = list.size();
		return  resultCount>0?true:false;
	}

	public List<UserModel> countUserInSameArea(Integer areaId) {
		String sql = "select * from user where departmentid between 2 and 33 and areaid = ?";
		return this.searchAsBeanList(sql, UserModel.class, areaId);
	}
}